<?php

/**
 * category actions.
 *
 * @package    product
 * @subpackage category
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class categoryActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
	$this->cTitle = '分类管理' ;
	$cr = new Criteria();
	$cr->add(ProductCategoryPeer::FID, 0);
	$this->categories = ProductCategoryPeer::doSelect($cr);
	$this->count = count($this->categories);
  }
  public function executeAjaxsub(sfWebRequest $request)
  {
  	$cr = new Criteria();
  	$fid = $this->getRequestParameter('fid',1);
	$cr->add(ProductCategoryPeer::FID, $fid);
	$this->categories = ProductCategoryPeer::doSelect($cr);
  }
  public function executeCreate(sfWebRequest $request)
  {
  	$this->cTitle = '分类管理' ;
  	$id = $this->getRequestParameter('fid',0);
  	if($id){
  		//create subclass
  		$form = new ProductCategoryForm();
  		$form->setDefault('fid',$id);
  		$form->setDefault('depth',1);  		
  	}else{
  		//create father class
  		$form = new ProductCategoryForm();  
  		$form->setDefault('fid',0);	
  		$form->setDefault('depth',0);	  		
  	}  	
  	$form->setDefault('orders',0);
  	$this->form = $form ;
  }
  public function executeViewsub(sfWebRequest $request)
  {
  	$id = $this->getRequestParameter('id',0);
  	if($id){
  		$this->id = $id;
	  	$cr = new Criteria();
		$cr->add(ProductCategoryPeer::FID, $id);
		$this->subcate = ProductCategoryPeer::doSelect($cr);
  	}else{
  		$this->subcate = '';
  	}  	
  }
  public function executeSave(sfWebRequest $request)
  {
  	if($request->isMethod('post')){
  		$this->form = new ProductCategoryForm();
  		$this->form->bind($request->getParameter('cate'));
  		if ($this->form->isValid())	{
		  //save the content
		  $fid = $this->form->getValue('fid');
		  $this->form->save();
		  $this->redirect('category/index?fid='.$fid);
		}else{
		  $this->redirect('category/index');
		}
  	}else{
  		$this->redirect('category/index');
  	}
  }
  public function executeEsave(sfWebRequest $request)
  {
   	if($request->isMethod('post')){  		
  		$array = $request->getParameter('cate');
  		$id = $array['id'];
  		$cate = ProductCategoryPeer::retrieveByPK($id);
  		$cate->setTitle($array['title']);
  		$cate->setFid($array['fid']);
  		if($cate->getFid()>0){
  			$cate->setDepth(1);
  		}else{
  			$cate->setDepth(0);
  		}
  		$cate->setIson($array['ison']);
  		$cate->setOrders($array['orders']);
  		$cate->save();
  		$this->redirect('category/index');
  	}else{
  		$this->redirect('category/index');
  	} 	
  }
  public function executeEdit(sfWebRequest $request)
  {
  	$this->cTitle = '分类管理' ;
  	$id = $this->getRequestParameter('id',0);
  	$this->cate = ProductCategoryPeer::retrieveByPK($id);
  	
  	$cr = new Criteria();
  	$cr->add(ProductCategoryPeer::FID,0);
  	$this->f_cate = ProductCategoryPeer::doSelect($cr);
  }
  public function executeDelete(sfWebRequest $request)
  {
  	$this->cTitle = "删除类别";
  	//only for subclass
  	$id = $this->getRequestParameter('id',0);
  	if($id){
  		//if this class exist
	  	$cate = ProductCategoryPeer::retrieveByPK($id);
	  	if(!$cate){
	  		$this->redirect('category/index');
	  	}
		if($cate->getFid()==0){
			$this->redirect('category/index');
		}
	    //if this is a father class, delete 
	  	$cr = new Criteria();  	
	  	$cr->add(ProductPeer::CATEGORY ,$cate->getId());
	  	$count = ProductPeer::doDelete($cr);
	  	$cate->delete();
	  	$message = $count.'条产品记录被删除了.<br />';
	  	$message.="1个产品类别被删除。";
	  	$this->message = $message ;
  	}else{
  		//if not exists;
  		$this->redirect('category/index');
  	}
  	
  }
}
